<template>
  <div class="form-container">
    <div class="container">
      <!--  主题内容标题   -->
      <div class="flex contain-header">
        <div class="contain-title">修改用户密码</div>
      </div>
      <!--  基本信息显示    -->
      <div class="info-contain">
        <div>
          <a-avatar :src="'http://zctuchuang.oss-cn-shenzhen.aliyuncs.com/'+avatar" :size="70"/>
          <div class="userName">{{ userName }}</div>
        </div>
        <a-form class="info-header" :form="form" @submit="submitInfo" :label-col="{ span: 6 }" :wrapper-col="{ span: 17,offset: 1 }">
          <a-form-item label="原密码">
            <a-input-password placeholder="请输入原密码" allowClear  v-decorator="['oldPwd', { rules: [{ required: true, message: '请输入原密码!' }] }]"/>
          </a-form-item>
          <a-form-item label="新密码">
            <a-input-password placeholder="请输入新密码" allowClear  v-decorator="['newPwd', { rules: [{ required: true, message: '请输入新密码!' }] }]"/>
          </a-form-item>
          <a-form-item label="确认密码">
            <a-input-password placeholder="请再次输入新密码" allowClear  v-decorator="['newPwdAgain', { rules: [{ required: true, message: '请再次输入新密码!' }] }]"/>
          </a-form-item>
          <a-row class="row btn-margin info-header">
            <div>
              <template>
                <a-button type="primary" html-type="submit">提交</a-button>
              </template>
              <a-button class="btn-space" @click="handleReset">重置</a-button>
            </div>
          </a-row>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script>
import {changePwd} from "@/api";

export default {
  name: "ChangePwd",
  data() {
    return{
      form: this.$form.createForm(this, { name: 'coordinated' }),
      avatar: window.localStorage.getItem('avatar'),
      userName: window.localStorage.getItem('userName')
    }
  },
  methods: {
    //修改用户密码
    submitInfo(e) {
      let data
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          data = this.form.getFieldsValue()
          data.userId = window.localStorage.getItem('userId')
          if(data) {
            changePwd(data)
              .then((res) => {
                if (res.status == 200){
                  this.$message.success('修改密码成功，请重新登录！');
                  this.handleReset()
                  this.$router.push('/login')
                }else{
                  this.$message.info(res.msg);
                  this.handleReset()
                }
              })
              .catch((err) => {
                this.$message.error(err);
                this.handleReset()
              })
          }
        }
      });
    },
    handleReset() {
      this.form.resetFields();
    }
  }
}
</script>

<style lang="less" scoped>
  .flex{
    display: flex;
    flex-direction: row;
  }
  .form-container {
    display: flex;
    justify-content: center;
  }
  .form-contain {
    width: 1600px;
    padding: 0px 30px 0px 30px;
    border-radius: 10px;
    border: 1px solid #5DD5C8;
    margin: 50px 0px;
  }
  .container{
    width: 1032px;
    height: 100%;
    background-color: #ffffff;
    border-radius: 4px;
    border: 1px solid #eeeeee;
    padding: 10px 13px;
    margin: 50px 0px;
    padding-bottom: 60px;
  }
  .contain-header{
    justify-content: space-between;
    align-items: center;
  }
  .contain-title{
    font-family: MicrosoftYaHei;
    font-size: 18px;
    font-weight: bold;
    font-stretch: normal;
    letter-spacing: 1px;
    color: #1bcbb4;
  }
  .contain-left{
    align-items: center;
    cursor: pointer;
  }
  .title{
    font-weight: 600;
    font-size: 16px;
    margin-right: 10px;
  }
  .info-contain {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 16px !important;
    color: #686767;
    margin-top: 30px;
  }
  .info-header {
    margin-top: 40px;
  }
  .userName {
    font-weight: 600;
    font-size: 18px;
    text-align: center;
    margin-top: 8px;
  }
  .text_center {
    text-align: center;
  }
  .info-content {
    width: 600px;
  }
  .row-style {
    margin-top: 18px;
  }
  .nav-icon{
    color: #999999;
    font-size: 20px;
    text-align: center;
  }
  .num-style {
    color: #1bcbb4;
    font-weight: bold;
    font-size: 24px;
    cursor: pointer;
  }
  .num-style:hover {
    color: #65dbcb;
  }
  .form-contain {
    width: 600px;
    padding: 0px 20px 20px 20px;
    border-radius: 10px;
    border: 1px solid #1bcbb4;
    margin: 50px 0px;
  }
  .form-title {
    font-size: 20px;
    font-weight: bold;
    text-align: center;
    padding: 5px;
    border-bottom: 1px solid #97f3e9;
  }
  .border-right {
    border-right: 1px solid #d6dcdc;
  }
  .record-space {
    margin-top: 10px;
  }
  .row{
    display: flex;
    flex-direction: row;
    justify-items: center;
    justify-content: center;
    margin-bottom: 15px;
  }
  .btn-space{
    margin-left: 80px;
  }
  .btn-margin{
    margin-top: 30px;
  }
</style>